package com.rhb.mail.mail.web.struts.action;

import java.util.HashSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.*;

import com.rhb.exception.DuplicateException;
import com.rhb.mail.mail.bean.*;
import com.rhb.mail.mail.web.struts.form.*;
import com.rhb.mail.web.struts.action.MailBaseDispatchAction;

public class MailSaveAction extends MailBaseDispatchAction {
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		//System.out.println("unspecified for defalut parameter!");
		return mapping.findForward("fail");
	}
	public ActionForward erase(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MailForm oform = (MailForm)form;
		this.getMailBusiness().erase(oform.getMail());
		return mapping.findForward("successErase");
	}

	public ActionForward del(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MailForm oform = (MailForm)form;
		this.getMailBusiness().delete(oform.getMail());
		//System.out.println("********* mailSave.del() *******");
		return mapping.findForward("success");
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MailForm oform = (MailForm)form;

		//-- xgrid
		oform.getMail().getMailItems().clear();
		oform.getMail().getMailItems().addAll(this.generateItems(oform.getReceiver(),oform.getReceiverPk()));
		
		ActionMessages messages = new ActionMessages();
		try {
			this.getMailBusiness().update(oform.getMail());
		} catch (DuplicateException e) {
			messages.add("errors.duplicate", new ActionMessage("errors.duplicate",e.toString()));
			saveErrors(request, messages);
			return new ActionForward(mapping.getInput());
		}
		return mapping.findForward("success");
	}
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MailForm oform = (MailForm)form;

		//-- xgrid
		oform.getMail().getMailItems().clear();
		oform.getMail().getMailItems().addAll(this.generateItems(oform.getReceiver(),oform.getReceiverPk()));
		
		ActionMessages messages = new ActionMessages();
		try {
			this.getMailBusiness().create(oform.getMail());
		} catch (DuplicateException e) {
			messages.add("errors.duplicate", new ActionMessage("errors.duplicate",e.toString()));
			saveErrors(request, messages);
			return new ActionForward(mapping.getInput());
		}
		return mapping.findForward("success");
	}
	
	public ActionForward resend(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MailForm oform = (MailForm)form;

		oform.getMail().getMailItems().clear();
		//oform.getMail().getMailItems().addAll(this.getItems(oform.getItems()));

		ActionMessages messages = new ActionMessages();
		try {
			this.getMailBusiness().create(oform.getMail());
		} catch (DuplicateException e) {
			messages.add("errors.duplicate", new ActionMessage("errors.duplicate",e.toString()));
			saveErrors(request, messages);
			return new ActionForward(mapping.getInput());
		}
		
		//System.out.println("****** after resend saved, the behavior is " + oform.getBehavior());
		//System.out.println("****** after resend saved, the preBehavior is " + oform.getPreBehavior());
		//System.out.println("****** after resend saved, the behavior_history is " + oform.getBehavior_history());
		
		return mapping.findForward("success");
	}
	
	public ActionForward reply(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MailForm oform = (MailForm)form;

		oform.getMail().getMailItems().clear();

		ActionMessages messages = new ActionMessages();
		try {
			this.getMailBusiness().create(oform.getMail());
		} catch (DuplicateException e) {
			messages.add("errors.duplicate", new ActionMessage("errors.duplicate",e.toString()));
			saveErrors(request, messages);
			return new ActionForward(mapping.getInput());
		}
		
		//System.out.println("****** after reply saved, the behavior is " + oform.getBehavior());
		//System.out.println("****** after reply saved, the preBehavior is " + oform.getPreBehavior());
		//System.out.println("****** after reply saved, the behavior_history is " + oform.getBehavior_history());
		
		return mapping.findForward("success");
	}
	
	//--xgrid
	private HashSet generateItems(String receiver, String receiverPk){
		HashSet aItems = new HashSet();
		String[] er = receiver.split(",");
		String[] pk = receiverPk.split(",");
		
		if(er != null && er.length!=0){
			for(int i=0; i<er.length; i++){
				MailItem ai = new MailItem();
				ai.setReceivedPersonPk(pk[i]);
				ai.setReceivedPersonName(er[i]);
				ai.setDeleted(0);
				ai.setReaded(0);
				aItems.add(ai);					
			}
		}
		return aItems;
	}
}
